View Javadoc
1   package edu.jiangxin.apktoolbox.convert.color.colorspace;
2   
3   import java.awt.*;
4   import java.awt.color.ColorSpace;
5   
6   public class HsbColorSpace extends ColorSpace {
7   
8       protected HsbColorSpace(int type, int numComponents) {
9           super(type, numComponents);
10      }
11  
12      public static HsbColorSpace getInstance() {
13          return Holder.INSTANCE;
14      }
15  
16      @Override
17      public float[] toRGB(float[] colorvalue) {
18          int rgb = Color.HSBtoRGB(colorvalue[0], colorvalue[1], colorvalue[2]);
19          return new float[]{
20                  (rgb >> 16 & 0xFF) / 255f,
21                  (rgb >> 8 & 0xFF) / 255f,
22                  (rgb & 0xFF) / 255f
23          };
24      }
25  
26      @Override
27      public float[] fromRGB(float[] rgbvalue) {
28          int r = (int) (rgbvalue[0] * 255);
29          int g = (int) (rgbvalue[1] * 255);
30          int b = (int) (rgbvalue[2] * 255);
31          return Color.RGBtoHSB(r, g, b, null);
32      }
33  
34      @Override
35      public float[] toCIEXYZ(float[] colorvalue) {
36          float[] rgb = toRGB(colorvalue);
37          return ColorSpace.getInstance(CS_sRGB).toCIEXYZ(rgb);
38      }
39  
40      @Override
41      public float[] fromCIEXYZ(float[] colorvalue) {
42          float[] rgb = ColorSpace.getInstance(CS_sRGB).fromCIEXYZ(colorvalue);
43          return fromRGB(rgb);
44      }
45  
46      private static class Holder {
47          private static final HsbColorSpace INSTANCE = new HsbColorSpace(TYPE_HSV, 3);
48      }
49  }